package com.wonders.qxmanager.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wonders.qxmanager.base.Parameter;
import com.wonders.qxmanager.model.Log;
import com.wonders.qxmanager.model.User;
import com.wonders.qxmanager.service.LogService;
import com.wonders.qxmanager.service.UserService;
import com.wonders.qxmanager.utils.PageArrayList;
import com.wonders.qxmanager.utils.PageUIHelper;
import com.wonders.qxmanager.utils.PageUIHelper.Page;

@Controller
public class LogCrotroller {

	@Autowired
	private LogService logService;
	List<User> list;

	@RequestMapping("/log/save.do")
	public void saveLog(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		req.setCharacterEncoding("UTF-8");
		Log log = new Log();
		log.setUserId(Integer.parseInt(req.getParameter("userId")));
		log.setWhat(req.getParameter("what"));
		log.setWhatTime(req.getParameter("whatTime"));
		log.setWhatResult(req.getParameter("whatResult"));
		logService.addLog(log);

		PageUIHelper.writeFormResponse(res, "{errorMsg:true}");

	}

	@RequestMapping("/log/delete.do")
	public void remove(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		System.out.println(req.getParameter("id"));
		logService.deleteLog(Integer.parseInt(req.getParameter("id")));
		PageUIHelper.writeFormResponse(res, "{errorMsg:true}");
	}
	
	
	@RequestMapping("/log/edit.do")
	public void edit(Log log, HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		req.setCharacterEncoding("UTF-8");
		
		String sqledit = "UPDATE Log set userId=:p1,what=:p2,whatTime=:p3,whatResult=:p4 WHERE id=:p0";
		Parameter parameter1 = new Parameter();
		parameter1.put("p0", log.getId());
		parameter1.put("p1", log.getUserId());
		parameter1.put("p2", log.getWhat());
		parameter1.put("p3", log.getWhatTime());
		parameter1.put("p4", log.getWhatResult());
		logService.updateLog(sqledit, parameter1);
		PageUIHelper.writeFormResponse(res, "{errorMsg:true}");
	}
	
	@SuppressWarnings("null")
	@RequestMapping("/log/list.do")
	public void list(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		HttpSession session = req.getSession();
		req.setCharacterEncoding("UTF-8");
		Log log = new Log();
		PageArrayList<Log> list = null;
		Page page = PageUIHelper.getPage(req);
		String itemid = req.getParameter("itemid");
		System.out.println(itemid);
		if (itemid != null && StringUtils.isNotBlank(itemid)) {
			String sqlbyid = "from Log where what=:p0";
			Parameter parameter1 = new Parameter();
			parameter1.put("p0", itemid);
			
			log = logService.getLogByHQL(sqlbyid, parameter1);
			System.out.println(log.getWhat().toString() + "***********");
			// 乐乐提示
			list = new PageArrayList<Log>(1, 1, 1);
			list.add(log);
			// list = userService.getListByPage(user, page);
		} else {
			list = logService.getListByPage(log, page);

		}
		PageUIHelper.writeJsonResponse(res, PageUIHelper.toString(list));
	}

}
